
function verifyUserName(){
    const tmp = $("#username").val();
    if (tmp == "") {
        document.getElementById("un").innerHTML="<font color='red'>用户名不能为空</font>";
        document.getElementById("submit").disabled = true;
        return false;
    }else if(!(/^[a-z0-9_-]{3,15}$/.test(tmp))){
        document.getElementById("un").innerHTML="<font color='red'>用户名长度在3-16位之间<br/>不能包含除了'-','_'以外的特殊字符</font>";
        document.getElementById("submit").disabled = true;
        return false
    } else {
        // 这里应该不做反应，或是与if进行反向操作
        document.getElementById("un").innerHTML="";
    }
    console.log(tmp)
    $.ajax({
        type: "get",
        url:"/verifyUserName",
        async:false,
        data:{
            username:$("#username").val()
        },
        success(data){
            console.log(data)
            if (data == false){
                document.getElementById("un").innerHTML="<font color='red'>用户名已存在</font>";
                document.getElementById("submit").disabled = true;
            }
        },
        error(){
            console.log("error")
            // alert("ajax配置有错，请检查url与type是否正确");
        }
    });
}
function verifyUserPhone() {
    const tmp = $("#phone").val();
    if (tmp == "") {
        document.getElementById("ph").innerHTML = "<font color='red'>电话号码不能为空</font>";
        document.getElementById("submit").disabled = true;
        return false;
    }else if(!(/^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/.test(tmp))){
        document.getElementById("ph").innerHTML = "<font color='red'>电话号码不合规</font>";
        document.getElementById("submit").disabled = true;
        return false
    } else {
        // 这里应该不做反应，或是与if进行反向操作
        document.getElementById("ph").innerHTML = "";
        document.getElementById("submit").disabled = false;
    }

    console.log(tmp)
    $.ajax({
        type: "get",
        data:{
            phone:$("#phone").val()
        },
        url:"/verifyUserPhone",
        async:false,
        success(data){
            console.log(data)
            if (data == false){
                document.getElementById("ph").innerHTML="<font color='red'>电话号码已存在</font>";
                document.getElementById("submit").disabled = true;
            }
        },
        error(){
            console.log("error")
            // alert("ajax配置有错，请检查url与type是否正确"+data);
            console.log(this.data)
        }
    });
}

function verifyUserEmail() {
    const tmp = $("#email").val();
    if (tmp == "") {
        document.getElementById("em").innerHTML="<font color='red'>邮箱不能为空</font>";
        document.getElementById("submit").disabled = true;
        return false;
    }else if(!(/^[a-zA-Z0-9_\-]{2,}@[a-zA-Z0-9_\-]{2,}(\.[a-zA-Z0-9_\-]+){1,2}$/.test(tmp))){
        document.getElementById("em").innerHTML="<font color='red'>邮箱格式不正确</font>";
        document.getElementById("submit").disabled = true;
        return false;
    } else {
        // 这里应该不做反应，或是与if进行反向操作
        document.getElementById("em").innerHTML="";
        document.getElementById("submit").disabled = false;
    }
    console.log(tmp)
    $.ajax({
        type: "get",
        data:{
            email:$("#email").val()
        },
        url:"/verifyUserEmail",
        async:false,
        success(data){
            console.log(data)
            if (data == false){
                document.getElementById("em").innerHTML="<font color='red'>邮箱已存在</font>";
                document.getElementById("submit").disabled = true;
            }
        },
        error(){
            // alert("ajax配置有错，请检查url与type是否正确");
            alert("error");
            console.log(this.data)
        }
    });
}

function validatePwd() {
    var pwd1 = document.getElementById("password").value;
    var pwd2 = document.getElementById("confirmPassword").value;

    if(pwd1 == "" || pwd2 == ""){
        document.getElementById("hint").innerHTML="<font color='red'>请输入密码</font>";
        document.getElementById("submit").disabled = true;
        return false
    }

    <!-- 对比两次输入的密码 -->
    if(pwd1 == pwd2) {
        document.getElementById("hint").innerHTML="<font color='green'></font>";
        document.getElementById("submit").disabled = false;
    }
    else {
        document.getElementById("hint").innerHTML="<font color='red'>两次密码不相同</font>";
        document.getElementById("submit").disabled = true;
        return false
    }

    //强度校验
    // if(!(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/.test(pwd1))){
    if(!(/^(?=.*?[a-z])(?=.*?[0-9]).{6,20}$/.test(pwd1))){
        document.getElementById("hint").innerHTML="<font color='red'>密码要求至少包含数字和英文，长度6-20之间</font>";
        document.getElementById("submit").disabled = true;
        return false
    }
}

function checkAll(){
    if (verifyUserName() || verifyUserPhone() || verifyUserEmail() || validatePwd() == false){
        $('#register_form').on('submit',function (event){
            event.preventDefault()
        })
        document.getElementById("submit").disabled = true;
        return false
    }else {
        $.ajax({
            type: "post",
            data:{
                'username':$("#username").val(),
                'password':$('#password').val(),
                'phone': $("#phone").val(),
                'email':$("#email").val()
            },
            url:"/register",
            async:false,
            success(data){
                console.log(data)
                alert('注册成功')
                window.location.href = '/login'
            },
            error(){
                // alert("ajax配置有错，请检查url与type是否正确");
                alert("注册异常");
                console.log(this.data)
            }
        });
    }
}
